<?php
namespace app\web\controller;
use app\common\controller\StoreBaseController;
use app\wechat\model\WechatMenu as WechatMenuModel;
/**
 * 微信菜单
 */
class WechatMenuController extends StoreBaseController {

	protected $menu_model;

	protected function _initialize() {
		parent::_initialize();
		$this -> menu_model = new WechatMenuModel();
	}

	/**
	 * 后台菜单
	 * @return mixed
	 */
	public function index() {
		$store_id = session('storeInfo')['id'];
		$menu_list = $this -> menu_model -> where('mid', $store_id) -> order('sort') -> select();
		$list = array2level($menu_list);
		$this -> assign('list', $list);
		return $this -> fetch();
	}

	/**
	 * 添加菜单
	 * @param string $pid
	 * @return mixed
	 */
	public function add($pid = '') {
		$store_id = session('storeInfo')['id'];
		$menu_list = $this -> menu_model -> where('pid', 0) -> where('mid', $store_id) -> order('sort') -> select();
		$this -> assign('list', $menu_list);
		if ($this -> request -> isPost()) {
			$data = $this -> request -> post();
			$data['mid'] = $store_id;
			// 判断是否超过3个顶级菜单、5个二级菜单
			$msg = $this -> checkMenu($pid);
			if ($msg) {
				$this -> error($msg);
			}
			if ($this -> menu_model -> save($data)) {
				$this -> success('保存成功', url('wechat_menu/index'));
			} else {
				$this -> error('保存失败');
			}
		} else {
			$this -> assign('pid', $pid);
			return $this -> fetch();
		}
	}

	/**
	 * 更新菜单
	 * @param $id
	 */
	public function update($id) {
		$store_id = session('storeInfo')['id'];
		$menu_list = $this -> menu_model -> where('pid', 0) -> where('mid', $store_id) -> order('sort') -> select();
		$this -> assign('list', $menu_list);
		if ($this -> request -> isPost()) {
			$data = $this -> request -> post();
			if ($this -> menu_model -> save($data, $id) !== false) {
				$this -> success('更新成功', url('wechat_menu/index'));
			} else {
				$this -> error('更新失败');
			}
		} else {
			$admin_menu = $this -> menu_model -> find($id);
			$this -> assign('model', $admin_menu);
			return $this -> fetch();
		}
	}

	/**
	 * 删除菜单
	 * @param $id
	 */
	public function delete($id) {
		if ($this -> menu_model -> destroy($id)) {
			$this -> success('删除成功');
		} else {
			$this -> error('删除失败');
		}
	}

	public function upload() {
		// 查出一级菜单
		$menu_list = $this -> menu_model -> where('pid', 0) -> where('mid', 0) -> order('sort') -> select();
		// 菜单集合
		$new_menu = array();
		$count = count($menu_list);
		for ($i = 0; $i < $count; $i++) {
			$child = $this -> menu_model -> where('pid', $menu_list[$i]['id']) -> order('sort') -> select();
			// 添加二级菜单
			if ($child) {
				foreach ($child as $vo) {
					$c_menu = array();
					$c_menu['name'] = $vo['name'];
					$c_menu['type'] = $vo['type'];
					if ($c_menu['type'] == 'view') {
						$c_menu['url'] = $vo['val'];
					} else {
						$c_menu['key'] = $vo['val'];
					}
					$new_menu[$i]['name'] = $menu_list[$i]['name'];
					$new_menu[$i]['type'] = $menu_list[$i]['type'];
					if ($new_menu[$i]['type'] == 'view') {
						$new_menu[$i]['url'] = $menu_list[$i]['val'];
					} else {
						$new_menu[$i]['key'] = $menu_list[$i]['val'];
					}
					$new_menu[$i]['sub_button'][] = $c_menu;
				}
			} else {
				$new_menu[$i]['name'] = $menu_list[$i]['name'];
				$new_menu[$i]['type'] = $menu_list[$i]['type'];
				if ($new_menu[$i]['type'] == 'view') {
					$new_menu[$i]['url'] = $menu_list[$i]['val'];
				} else {
					$new_menu[$i]['key'] = $menu_list[$i]['val'];
				}
			}
		}
		$we_menu = load_wechat('menu');
		$add_menu['button'] = $new_menu;
		// 创建微信菜单
		$result = $we_menu -> createMenu($add_menu);
		// 处理创建结果
		if ($result === FALSE) {
			// 接口失败的处理
			$this -> error($we_menu -> errMsg);
		} else {
			$this -> success('更新成功！');
		}
	}

	/**
	 * 判断菜单是否超过限制
	 */

	private function checkMenu($pid = 0) {
		$store_id = session('storeInfo')['id'];
		$count = $this -> menu_model -> where('pid', $pid)->where('mid',$store_id) -> count();
		// 顶级菜单最多3个
		if ($pid == 0) {
			if ($count >= 3) {
				return '一级菜单最多3个';
			}
		} else {
			if ($count >= 5) {
				return '二级菜单最多5个';
			}
		}
		return 0;
	}

}
